package com.cn.common.core.session;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.cn.common.core.FastJsonUtils;
import com.cn.common.core.model.Response;
import com.cn.common.core.model.WebStockResponse;
import com.cn.common.core.serial.Serializer;
import com.cn.common.core.session.Session;
import com.google.protobuf.GeneratedMessage;

/**
 *
 */
public class SessionManager {

    /**
     * 在线会话
     */
    private static final ConcurrentHashMap<String, Session> onlineSessions = new ConcurrentHashMap<>();

    /**
     * 加入
     *
     * @param loginName
     * @param session
     * @return
     */
    public static boolean putSession(String loginName, Session session) {
        if (!onlineSessions.containsKey(loginName)) {
            boolean success = onlineSessions.putIfAbsent(loginName, session) == null ? true : false;
            return success;
        }
        return false;
    }

    /**
     * 移除
     *
     * @param loginName
     */
    public static Session removeSession(String loginName) {
        return onlineSessions.remove(loginName);
    }

    /**
     * 发送消息[自定义协议]
     *
     * @param <T>
     * @param loginName
     * @param message
     */
    public static <T extends Serializer> void sendMessage(String loginName, short module, short cmd, T message) {
        Session session = onlineSessions.get(loginName);
        if (session != null && session.isConnected()) {
            if (session.getType() == Session.Type.MOBILE) {
                sendMessage(session, module, cmd, message);
            } else if (session.getType() == Session.Type.WEBSOCKET) {
                sendWebSocketMessage(session, module, cmd, message);
            }
        }
    }


    /**
     * 发送客户端
     *
     * @param session
     * @param module
     * @param cmd
     * @param message
     * @param <T>
     */
    private static <T extends Serializer> void sendMessage(Session session, short module, short cmd, T message) {
        if (session != null && session.isConnected()) {
            Response response = new Response(module, cmd, message.getBytes());
            session.write(response);
        }
    }

    /**
     * 发送给网页websocket
     *
     * @param session
     * @param module
     * @param cmd
     * @param message
     * @param <T>
     */
    private static <T> void sendWebSocketMessage(Session session, short module, short cmd, T message) {
        if (session != null && session.isConnected()) {
            WebStockResponse response = new WebStockResponse(module, cmd, FastJsonUtils.toJson(message));
            session.write(response);
        }
    }


    /**
     * 发送消息[protoBuf协议]
     *
     * @param <T>
     * @param loginName
     * @param message
     */
    public static <T extends GeneratedMessage> void sendMessage(String loginName, short module, short cmd, T message) {
        Session session = onlineSessions.get(loginName);
        if (session != null && session.isConnected()) {
            Response response = new Response(module, cmd, message.toByteArray());
            session.write(response);
        }
    }

    /**
     * 是否在线
     *
     * @param loginName
     * @return
     */
    public static boolean isOnlineUser(String loginName) {
        return onlineSessions.containsKey(loginName);
    }

    /**
     * 获取所有在线用户
     *
     * @return
     */
    public static Set<String> getOnlinePlayers() {
        return Collections.unmodifiableSet(onlineSessions.keySet());
    }
}
